<?php

// EventTypes Widget 0.0.2 for the SilverStripe Diary Module
// 17.07.2010
// Author:  nivanka@silverstripers.com
// Save this and EventTypesWidget.ss to widget_eventtypes/ and run "db/build".
// If you are using this widget outside the calendarholder page 
// add the following code to your mysite/code/Page.php file

/*
	function type(){
		Session::set("EventType",$_GET['name']);
		$cal = DataObject::get_one('CalendarHolder');
		Director::redirect($cal->URLSegment . "/");
	}
*/



class EventTypesWidget extends Widget{
	
	static $db = array();
	
	static $title = "Event Types";
	static $cmsTitle = "Event Types";
	static $description = "Shows the event types of the calendars.";
	
	/*
	 * Get all the event types
	*/
	function getEventTypes(){
		
		Requirements::css("diary/css/eventtypes.css");		
		
		$allTypes = array();
		
		$events = DataObject::get("DiaryEvent");
		
		if(!$events){
			return;
		}
		
		foreach($events as $event){
			if(!isset($allTypes[$event->EventType]))
				$allTypes[strtolower($event->EventType)] = strtolower($event->EventType);
		}
		$output = new DataObjectSet();
		$calendar = DataObject::get_one("DiaryHolder");
		$output->push(
			new ArrayData(
				array(
					"Type" => "All Types",
					"Link" => $calendar->Link() ."/type/all",
					"Class" => "alltypes"
				)
			)
		);
		
		foreach($allTypes as $name => $value){
			$output->push(
				new ArrayData(
					array(
						"Type" => "$value",
						"Link" => $calendar->Link() ."/type/$value",
						"Class" => "othertype"
					)
				)
			);
		}
		return $output;
	}

}

